package com.mqtt.adapter;

import android.support.v7.widget.AppCompatImageView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mqtt.R;
import com.mqtt.dao.MQTTBean;

import java.util.List;

/**
 * @Author xiaowen
 * @Email xiaowen.wxf@google.com
 * @Date 2018/10/15 .
 * @Description This is HomeMqttAdapter.
 */

public class HomeMqttAdapter extends BaseQuickAdapter<MQTTBean, BaseViewHolder> implements View.OnClickListener {

    public HomeMqttAdapter(int layoutResId) {
        super(layoutResId);
    }

    private boolean isShowDeleteIcon = false;

    @Override
    protected void convert(BaseViewHolder helper, MQTTBean item) {
        int color = item.getColor();
        helper.setBackgroundColor(R.id.item_color, color);
        helper.setText(R.id.item_color_text, item.getColorNickName());
        AppCompatImageView ivDeleteIcon = helper.getView(R.id.im_delete_item);
        ivDeleteIcon.setTag(item);
        ivDeleteIcon.setVisibility(isShowDeleteIcon ? View.VISIBLE : View.GONE);
        ivDeleteIcon.setOnClickListener(this);
    }

    /**
     *  设置是否显示删除图标
     * @param showDeleteIcon showDeleteIcon
     */
    public void setShowDeleteIcon(boolean showDeleteIcon) {
        isShowDeleteIcon = showDeleteIcon;
    }

    public boolean isShowDeleteIcon() {
        return isShowDeleteIcon;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.im_delete_item :
                doDeletetItem(view); // 点击了删除按钮
                break;
        }
    }

    /**
     *  处理删除item的事件
     * @param view
     */
    private void doDeletetItem(View view) {
        MQTTBean mqttBean  = (MQTTBean) view.getTag();
        List<MQTTBean> data = getData();
        mqttBean.delete(); // 在数据库删除该字段信息
        data.remove(mqttBean);
        notifyDataSetChanged();
    }
}
